package com.beagle.njddapp.weight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 *
 */
public class PayDialog extends Dialog {

    @BindView(R.id.tv_pay)
    TextView tvPay;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_wx)
    ImageView ivwx;
    @BindView(R.id.iv_al)
    ImageView inal;
    private OilOrderBean oilOrderBean;
    private PayCallback payCallback;
    private int type=1;
    private String price;

    public void setPayCallback(PayCallback payCallback) {
        this.payCallback = payCallback;
    }

    public PayDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pay);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);

        }
    }


    @OnClick({R.id.rl_al,R.id.rl_wx,R.id.tv_pay,R.id.container})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.container:
                dismiss();
                break;
            case R.id.rl_al:
                if(type==1){
                    return;
                }
                type=1;
                inal.setImageResource(R.drawable.check_true_green);
                ivwx.setImageResource(R.drawable.check_false_gray);
                break;
            case R.id.rl_wx:
                if(type==2){
                    return;
                }
                type=2;
                ivwx.setImageResource(R.drawable.check_true_green);
                inal.setImageResource(R.drawable.check_false_gray);
                break;
            case R.id.tv_pay:
                if(type==1){
                    payCallback.aliPay(oilOrderBean,price);
                }else {
                    if(isWeChatAppInstalled(getContext())){
                        payCallback.wxPay(oilOrderBean,price);
                    }else {
                        ToastUtils.showShort("请安装微信");
                    }

                }
                dismiss();
                break;

        }
    }

    public void setOilOrderBean(OilOrderBean oilOrderBean,String price) {
        this.oilOrderBean=oilOrderBean;
        this.price=price;
        tvTitle.setText("加油订单 "+oilOrderBean.getOrderNum());
        tvPay.setText(price+"元，立即支付");
    }
    public  interface PayCallback{
        void aliPay(OilOrderBean bean,String price);
        void wxPay(OilOrderBean bean,String price);
    }
    public static boolean isWeChatAppInstalled(Context context) {


        IWXAPI api = WXAPIFactory.createWXAPI(context, "wx3d87e26691a0338a");
        if(api.isWXAppInstalled() ) {
            return true;
        } else {
            final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
            if (pinfo != null) {
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.mm")) {
                        return true;
                    }
                }
            }
            return false;
        }
    }


}
